package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired(required = false)
    private UserService userService;

    @RequestMapping("/selectAll")
    @LogAnnotation(value = "展示所有用户",type = "selectAll")
    public Map selectAll(int page, int limit, String userName){

        return userService.selectAll(page,limit,userName);
    }

    @RequestMapping("/selectOne")
    @LogAnnotation(value = "展示单个用户",type = "selectOne")
    public User selectOne(int id){
        return userService.selectOne(id);
    }

    @RequestMapping("/insert")
    @LogAnnotation(value = "添加用户",type = "insert")
    public Map insert(User user){
        return userService.insert(user);
    }

    @RequestMapping("/delete")
    @LogAnnotation(value = "删除用户",type = "delete")
    public Map delete(int id){
        return userService.delete(id);
    }

    @RequestMapping("/update")
    @LogAnnotation(value = "更新用户",type = "update")
    public Map update(User user, MultipartFile file, HttpServletRequest request) throws IOException {
        //绝对路径
        if(file!=null){
            String realPath = request.getServletContext().getRealPath("/img/user/");//绝对路径
            //做重命名防止文件名字被覆盖
            String newFilename=new Date().getTime()+"_"+file.getOriginalFilename();

            file.transferTo(new File(realPath+"/"+newFilename));

            user.setUserImage("/img/user/"+newFilename);
            return userService.update(user);
        }else{
            return userService.updatea(user);
        }
    }


    //批量下载
    @RequestMapping("/download")
    @LogAnnotation(value = "导出用户数据",type = "download")
    public Map selectAlls(){
        return userService.selectAlls();
    }


    //更改用户状态的controller
    @RequestMapping("/updates")
    @LogAnnotation(value = "用户隐藏or展示",type = "updates")
    public Map updates(User user){

        System.out.println(user);
        return userService.updates(user);
    }



    //查找全部男女
    @RequestMapping("/selectSex")
    @LogAnnotation(value = "echarts展示男女",type = "selectSex")
    public List<Map> selectSex(){
        System.out.println(userService.selectSex());
        return userService.selectSex();
    }

    //地区
    @RequestMapping("/selectCity")
    @LogAnnotation(value = "echarts展示城市",type = "selectCity")
    public List<Map> selectCity(){
        System.out.println(userService.selectCity());
        List<Map> maps = userService.selectCity();
        return maps;
    }


    //注册的时间
    @RequestMapping("/selectDate")
    @LogAnnotation(value = "echarts展示注册时间",type = "selectDate")
    public Map selectDate(){
        Map map = userService.selectDate();
        System.out.println(map);
        return map;
    }


}
